/*
 * Copyright (C) 2008,2009,2010,2011  Paulo Cesar Pereira de Andrade.
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Authors:
 *	Paulo Cesar Pereira de Andrade
 */

#ifndef _efunction_h
#define _efunction_h

#include "etypes.h"

#define efunction_p(object)	(etype(object) == t_function)

#define emethod_name(record, symbol)					\
    (symbol->a_ctor ? record->name->name : symbol->name)

/*
 * Types
 */
struct efunction {
    esymbol_t	*name;
    ehash_t	*hash;		/* local variables */
    east_list_t	*code;
    east_list_t	*tail;
    eint32_t	 args;		/* number of arguments */
    eint32_t	 voffset;	/* varargs if non zero */
    eint32_t	 tryoff;	/* catch object offset */
    etag_t	*tag;		/* result and arguments */

    evector_t	*labels;	/* label table */

    eint32_t	 stack;		/* max stack length */

#define fun_chg		1	/* modifies aggregate, pointer or global */
#define fun_esc		2	/* takes a pointer or load element/field */
#define fun_jmp		4	/* non local jump or call method/pointer */
    eint32_t	 flags;

    eint32_t	 temps;		/* number of cse temporaries */

    evector_t	*loads;		/* symbol loads information */

    evector_t	*stores;	/* symbol stores information */

    ehash_t	*hsym;		/* symbol to bit offset mapping */
    evector_t	*vsym;		/* bit offset to symbol mapping */
    empz_t	 esc;		/* symbols with aggregate access */
    empz_t	 ptr;		/* symbols with address taken */
    empz_t	 set;		/* symbols set */

    ehash_t	*functions;	/* called functions */

    ehash_t	*table;		/* { name: symbol, value: (label . block) } */
    eobject_t	 gotos;		/* gotos in format (symbol . block) */
    evector_t	*catches;	/* symbols only visible in catch blocks */

    erecord_t	*record;	/* set if function is a class method */

#if JITVM
    eobject_t	 prolog;
    eobject_t	 epilog;
    euint32_t	 note : 1;
#endif

    euint32_t	 defined : 1;
};

/*
 * Prototypes
 */
extern void
init_function(void);

extern void
finish_function(void);

extern efunction_t *
enew_function(ebool_t define, esymbol_t *name);

extern efunction_t *
enew_method(eclass_t *record, esymbol_t *name);

extern efunction_t *
eget_method(eclass_t *record, esymbol_t *name);

extern void
eset_methods(eclass_t *record);

#if !JITVM
extern void
efinalize_functions(void);

extern void
efinalize_methods(void);
#endif

extern void
ereference_function(esymbol_t *symbol);

extern etag_t *
eapply_function(esymbol_t *symbol, eobject_t list);

extern etag_t *
eapply_reference(eobject_t object, eobject_t list);

extern etag_t *
eapply_method(ebool_t explicit, eobject_t object,
	      esymbol_t *symbol, eobject_t list);

extern void
efinalize_function(efunction_t *lambda);

#endif /* _efunction_h */
